/**
 * @author Reksoft / Vasiliy Beglyanin
 *
 */
package org.sensice.matrioshkaAC.core
import java.util.Date
import java.util.Calendar

class SheduleItem(inputString: String) {
  val sheduleString = inputString
  var hours = new Array[Boolean](24)
  var mins = new Array[Boolean](60)
  private var days = new Array[Boolean](31)
  private var months = new Array[Boolean](12)
  private var weekDays = new Array[Boolean](7)

  private val values: Array[String] = sheduleString.split(" ")
  if (values.length != 5) throw new IllegalArgumentException

  parseValues(mins, values(0))
  parseValues(hours, values(1))
  parseValues(days, values(2))
  parseValues(months, values(3))
  parseValues(weekDays, values(4))

  private def parseValues(dimension: Array[Boolean], str: String) {
    val rangePat = "^(\\d*\\d)-(\\d*\\d)$".r
    val divisionPat = "^((\\d*\\d)|\\*)/(\\d*\\d)$".r
    val intervals = str.split(",")
    for (digit <- intervals) {
      try {
        if (digit.equals("*")) {
          for (i <- 0 to dimension.length - 1) {
            dimension(i) = true
          }
        } else if (rangePat.findFirstIn(digit) != None) {
          val rangePat(low, high) = digit
          if ((low != null) && (high != null))
            for (i <- low.toInt to high.toInt) {
              dimension(i - 1) = true
            }
        } else if (divisionPat.findFirstIn(digit) != None) {
          val divisionPat(divident, a, divider) = digit
          println(a, divident, divider)
          if (divident.equals("*")) {
            for (i <- 0 to dimension.length - 1) {
              if ((i + 1) % divider.toInt == 0) dimension(i) = true
            }
          } else throw new IllegalArgumentException
        } else {
          dimension(digit.toInt - 1) = true
        }

      } catch {
        case t => throw new IllegalArgumentException
      }
    }
  }

  def isToday(date: Date): Int = {
    val calendar = Calendar.getInstance()
    calendar.setTime(date)
    calendar.setFirstDayOfWeek(Calendar.MONDAY)
    calendar.set(Calendar.SECOND, 0)
    val indxMin = calendar.get(Calendar.MINUTE) - 1
    var indxHour = calendar.get(Calendar.HOUR_OF_DAY) - 1
    val indxDate = calendar.get(Calendar.DAY_OF_MONTH) - 1
    val indxMonth = calendar.get(Calendar.MONTH) - 1
    val indxWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1

    if (!days(indxDate) || !months(indxMonth) || !weekDays(indxWeek)) {
      return -1
    }
    var indxMinReturn = -1
    for (i <- indxMin to mins.length - 1; if indxMinReturn == -1) {
      if (mins(i)) {
        indxMinReturn = i
      }
    }
    if (indxMinReturn == -1) {
      indxHour += 1
      for (i <- 0 to indxMin; if indxMinReturn == -1) {
        if (mins(i)) indxMinReturn = i
      }
    }
    var indxHourReturn = -1
    for (i <- indxHour to hours.length - 1; if indxHourReturn == -1) {
      if (hours(i)) indxHourReturn = i
    }
    if (indxHourReturn == -1) return -1
    val inputTime = date.getTime()
    val eventTime = date
    eventTime.setHours(indxHourReturn + 1)
    eventTime.setMinutes(indxMinReturn + 1)
    ((eventTime.getTime() - inputTime) / 1000).toInt
  }
}